home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / _LWPCookieJar.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  6KB  |  194 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Load / save to libwww-perl (LWP) format files.
  5.  
  6. Actually, the format is slightly extended from that used by LWP\'s
  7. (libwww-perl\'s) HTTP::Cookies, to avoid losing some RFC 2965 information
  8. not recorded by LWP.
  9.  
  10. It uses the version string "2.0", though really there isn\'t an LWP Cookies
  11. 2.0 format.  This indicates that there is extra information in here
  12. (domain_dot and # port_spec) while still being compatible with
  13. libwww-perl, I hope.
  14.  
  15. '''
  16. import time
  17. import re
  18. import logging
  19. from cookielib import reraise_unmasked_exceptions, FileCookieJar, Cookie, MISSING_FILENAME_TEXT, join_header_words, split_header_words, iso2time, time2isoz
  20.  
  21. def lwp_cookie_str(cookie):
  22.     '''Return string representation of Cookie in an the LWP cookie file format.
  23.  
  24.     Actually, the format is extended a bit -- see module docstring.
  25.  
  26.     '''
  27.     h = [
  28.         (cookie.name, cookie.value),
  29.         ('path', cookie.path),
  30.         ('domain', cookie.domain)]
  31.     if cookie.port is not None:
  32.         h.append(('port', cookie.port))
  33.     
  34.     if cookie.path_specified:
  35.         h.append(('path_spec', None))
  36.     
  37.     if cookie.port_specified:
  38.         h.append(('port_spec', None))
  39.     
  40.     if cookie.domain_initial_dot:
  41.         h.append(('domain_dot', None))
  42.     
  43.     if cookie.secure:
  44.         h.append(('secure', None))
  45.     
  46.     if cookie.expires:
  47.         h.append(('expires', time2isoz(float(cookie.expires))))
  48.     
  49.     if cookie.discard:
  50.         h.append(('discard', None))
  51.     
  52.     if cookie.comment:
  53.         h.append(('comment', cookie.comment))
  54.     
  55.     if cookie.comment_url:
  56.         h.append(('commenturl', cookie.comment_url))
  57.     
  58.     keys = cookie._rest.keys()
  59.     keys.sort()
  60.     for k in keys:
  61.         h.append((k, str(cookie._rest[k])))
  62.     
  63.     h.append(('version', str(cookie.version)))
  64.     return join_header_words([
  65.         h])
  66.  
  67.  
  68. class LWPCookieJar(FileCookieJar):
  69.     '''
  70.     The LWPCookieJar saves a sequence of"Set-Cookie3" lines.
  71.     "Set-Cookie3" is the format used by the libwww-perl libary, not known
  72.     to be compatible with any browser, but which is easy to read and
  73.     doesn\'t lose information about RFC 2965 cookies.
  74.  
  75.     Additional methods
  76.  
  77.     as_lwp_str(ignore_discard=True, ignore_expired=True)
  78.  
  79.     '''
  80.     
  81.     def as_lwp_str(self, ignore_discard = True, ignore_expires = True):
  82.         '''Return cookies as a string of "
  83. "-separated "Set-Cookie3" headers.
  84.  
  85.         ignore_discard and ignore_expires: see docstring for FileCookieJar.save
  86.  
  87.         '''
  88.         now = time.time()
  89.         r = []
  90.         for cookie in self:
  91.             if not ignore_discard and cookie.discard:
  92.                 continue
  93.             
  94.             if not ignore_expires and cookie.is_expired(now):
  95.                 continue
  96.             
  97.             r.append('Set-Cookie3: %s' % lwp_cookie_str(cookie))
  98.         
  99.         return '\n'.join(r + [
  100.             ''])
  101.  
  102.     
  103.     def save(self, filename = None, ignore_discard = False, ignore_expires = False):
  104.         if filename is None:
  105.             if self.filename is not None:
  106.                 filename = self.filename
  107.             else:
  108.                 raise ValueError(MISSING_FILENAME_TEXT)
  109.         
  110.         f = open(filename, 'w')
  111.         
  112.         try:
  113.             f.write('#LWP-Cookies-2.0\n')
  114.             f.write(self.as_lwp_str(ignore_discard, ignore_expires))
  115.         finally:
  116.             f.close()
  117.  
  118.  
  119.     
  120.     def _really_load(self, f, filename, ignore_discard, ignore_expires):
  121.         magic = f.readline()
  122.         if not re.search(self.magic_re, magic):
  123.             msg = '%s does not seem to contain cookies' % filename
  124.             raise IOError(msg)
  125.         
  126.         now = time.time()
  127.         header = 'Set-Cookie3:'
  128.         boolean_attrs = ('port_spec', 'path_spec', 'domain_dot', 'secure', 'discard')
  129.         value_attrs = ('version', 'port', 'path', 'domain', 'expires', 'comment', 'commenturl')
  130.         
  131.         try:
  132.             while None:
  133.                 line = f.readline()
  134.                 if line == '':
  135.                     break
  136.                 
  137.                 if not line.startswith(header):
  138.                     continue
  139.                 
  140.                 line = line[len(header):].strip()
  141.                 for data in split_header_words([
  142.                     line]):
  143.                     (name, value) = data[0]
  144.                     standard = { }
  145.                     rest = { }
  146.                     for k in boolean_attrs:
  147.                         standard[k] = False
  148.                     
  149.                     for k, v in data[1:]:
  150.                         if k is not None:
  151.                             lc = k.lower()
  152.                         else:
  153.                             lc = None
  154.                         if lc in value_attrs or lc in boolean_attrs:
  155.                             k = lc
  156.                         
  157.                         if k in boolean_attrs:
  158.                             if v is None:
  159.                                 v = True
  160.                             
  161.                             standard[k] = v
  162.                             continue
  163.                         if k in value_attrs:
  164.                             standard[k] = v
  165.                             continue
  166.                         rest[k] = v
  167.                     
  168.                     h = standard.get
  169.                     expires = h('expires')
  170.                     discard = h('discard')
  171.                     if expires is not None:
  172.                         expires = iso2time(expires)
  173.                     
  174.                     if expires is None:
  175.                         discard = True
  176.                     
  177.                     domain = h('domain')
  178.                     domain_specified = domain.startswith('.')
  179.                     c = Cookie(h('version'), name, value, h('port'), h('port_spec'), domain, domain_specified, h('domain_dot'), h('path'), h('path_spec'), h('secure'), expires, discard, h('comment'), h('commenturl'), rest)
  180.                     if not ignore_discard and c.discard:
  181.                         continue
  182.                     
  183.                     if not ignore_expires and c.is_expired(now):
  184.                         continue
  185.                     
  186.                     self.set_cookie(c)
  187.                 
  188.         except:
  189.             reraise_unmasked_exceptions((IOError,))
  190.             raise IOError('invalid Set-Cookie3 format file %s' % filename)
  191.  
  192.  
  193.  
  194.